package client;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;

public class EchoClient {
	public static void main(String[] args) {
		Socket s = null;
		BufferedReader in = null;// 读取文本信息的流,可逐行读取
		PrintWriter out = null; // 向文本输出流打印对象的格式化表示形式,此类实现在 PrintStream 中的所有
		// print 方法.可以逐行写出.这个输出流只能用于文本
		// InputStreamReader.桥梁流,将字节转换成字符.
		// OutputSteamWriter.桥梁流,将字符转换成字节.
		//
		try {
			s = new Socket("localhost", 9999);
			// s = new Socket("127.0.0.1",7610);
			System.out.println("连接OK");

			in = new BufferedReader(new InputStreamReader(s.getInputStream()));
			out = new PrintWriter(new OutputStreamWriter(s.getOutputStream()));
			while (true) {
				String input = prompt(">");
				out.println(input);
				out.flush();
				System.out.println("echo " + in.readLine());
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				in.close();
				s.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	private static String prompt(String message) {
		String input = "";
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		System.out.println(message);
		try {
			input = in.readLine();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return input;
	}

}
